import { Service } from '../../services/service'
import { formaturl, encryptPassword } from '@/utils/commonUtils'
const state = {
  token: '',
  loginStatus: '',
  loginInfo: {},
  loginCode: '',
  deviceId: '',
  errMsg: '',
  authorizedPage: ['/main/audit']
}
const getters = {}
const mutations = {
  setLoginStatus(state, params) {
    state.token = params.access_token
    localStorage.setItem('loginToken', state.token)
  },
  setErrorMsg(state, params) {
    state.errMsg = params.errMsg
  },
  setLoginInfo(state, params) {
    state.loginInfo = params
  },
  setDeviceId(state, id) {
    state.deviceId = id
  }
}
const actions = {
  async userLogin({ commit, state }, params) {
    params.password = encryptPassword(params.password)
    const mixedParams = {
      ...params,
      deviceId: state.deviceId
    }
    const retData = await Service({
      url: '/hanyun/authorization-server/oauth/token' + formaturl(mixedParams),
      method: 'post',
      data: {}
    })
    commit('setLoginStatus', retData)
    commit('home/setCurrenttab', '1', { root: true })
    return new Promise((resolve) => {
      resolve(retData)
    })
  },
  async getLoginInfo({ commit, state }) {
    const retData = await Service({
      url: '/hanyun/user-service/api/v1/user/findCurrentUser',
      method: 'get',
      data: {}
    })
    if (retData.status === 200) {
      commit('setLoginInfo', retData.result.user)
      sessionStorage.setItem('loginUserId', retData.result.user.id)
    }
  }
}
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
